#include "config.h"

Configuration::Configuration()
{
}

Configuration::~Configuration()
{
}

bool Configuration::Load(string filename)
{
	ifstream file;
	file.open(filename.c_str(), ios::in);
	
	/*if(file.good()) {*/
		char b;
		file.get(b);
		string category = "default";
		map<string, string> categoryValues;
		string key;
		string value;
		while(!file.eof()) {
			if(b == '[') {
				if(!categoryValues.empty())
					this->categories[category] = categoryValues;

				category = "";
				categoryValues.clear();

				while(true) {
					file.get(b);

					if(b == ']')
						break;
					else
						category += b;
				}
			} else if(b == '\n') {
				file.get(b);

				if(b == '[')
					continue;
				else
					key += b;

				while(true) {
					file.get(b);

					if(b == '=')
						break;
					else
						key += b;
				}
				while(true) {
					file.get(b);

					if(b == '\n')
						break;
					else
						value += b;
				}

				categoryValues[key] = value;
				key = "";
				value = "";
			}

			file.get(b);
		}

		file.close();

		return true;
	/*}
	
	return false;*/
}

map<string, string> Configuration::GetCategory(string category)
{
	return this->categories[category];
}

string Configuration::GetValue(string category, string key)
{
	return this->categories[category][key];
}